package com.ruoyi.web.controller.ism;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ism.domain.IsmActivityPublish;
import com.ruoyi.ism.domain.IsmCustManage;
import com.ruoyi.ism.domain.IsmIntegralRecord;
import com.ruoyi.ism.service.IIsmActivityPublishService;
import com.ruoyi.ism.service.IIsmCustManageService;
import com.ruoyi.ism.service.IIsmIntegralRecordService;
import net.bytebuddy.asm.Advice;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ism.domain.IsmActivitySign;
import com.ruoyi.ism.service.IIsmActivitySignService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 签到审核Controller
 * 
 * @author ruoyi
 * @date 2024-07-24
 */
@RestController
@RequestMapping("/ism/activitySign")
public class IsmActivitySignController extends BaseController
{
    @Autowired
    private IIsmActivitySignService ismActivitySignService;

    @Autowired
    private IIsmActivityPublishService iIsmActivityPublishService;

    @Autowired
    private IIsmCustManageService iIsmCustManageService;

    @Autowired
    private IIsmIntegralRecordService ismIntegralRecordService;

    /**
     * 查询签到审核列表
     */
    @PreAuthorize("@ss.hasPermi('ism:activitySign:list')")
    @GetMapping("/list")
    public TableDataInfo list(IsmActivitySign ismActivitySign)
    {
        startPage();
        List<IsmActivitySign> list = ismActivitySignService.selectIsmActivitySignList(ismActivitySign);
        return getDataTable(list);
    }

    /**
     * 导出签到审核列表
     */
    @PreAuthorize("@ss.hasPermi('ism:activitySign:export')")
    @Log(title = "签到审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IsmActivitySign ismActivitySign)
    {
        List<IsmActivitySign> list = ismActivitySignService.selectIsmActivitySignList(ismActivitySign);
        ExcelUtil<IsmActivitySign> util = new ExcelUtil<IsmActivitySign>(IsmActivitySign.class);
        util.exportExcel(response, list, "签到审核数据");
    }

    /**
     * 获取签到审核详细信息
     */
    @PreAuthorize("@ss.hasPermi('ism:activitySign:query')")
    @GetMapping(value = "/{signId}")
    public AjaxResult getInfo(@PathVariable("signId") Long signId)
    {
        return success(ismActivitySignService.selectIsmActivitySignBySignId(signId));
    }

    /**
     * 新增签到审核
     */
    @PreAuthorize("@ss.hasPermi('ism:activitySign:add')")
    @Log(title = "签到审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IsmActivitySign ismActivitySign)
    {
        return toAjax(ismActivitySignService.insertIsmActivitySign(ismActivitySign));
    }

    /**
     * 修改签到审核
     */
    @PreAuthorize("@ss.hasPermi('ism:activitySign:edit')")
    @Log(title = "签到审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IsmActivitySign ismActivitySign)
    {
        return toAjax(ismActivitySignService.updateIsmActivitySign(ismActivitySign));
    }

    /**
     * 删除签到审核
     */
    @PreAuthorize("@ss.hasPermi('ism:activitySign:remove')")
    @Log(title = "签到审核", businessType = BusinessType.DELETE)
    @DeleteMapping("/{signIds}")
    public AjaxResult remove(@PathVariable Long[] signIds)
    {
        return toAjax(ismActivitySignService.deleteIsmActivitySignBySignIds(signIds));
    }

    /**
     * 修改签到审核
     */
    @PreAuthorize("@ss.hasPermi('ism:activitySign:edit')")
    @Log(title = "签到审核", businessType = BusinessType.UPDATE)
    @PutMapping("/updateActivitySignPass")
    public AjaxResult updateActivitySignPass(@RequestBody IsmActivitySign ismActivitySign)
    {
        String activitySn = ismActivitySign.getActivitySn();
        String familySn = ismActivitySign.getFamilySn();
        String holderName = ismActivitySign.getHouseholderName();

        IsmActivitySign tmp = ismActivitySignService.selectActivitySignByfamilyInfo(ismActivitySign);
        if(tmp.getAuditFlat() !=0){
            return error("审核失败，非待审核状态");
        }

        //获取活动积分
        IsmActivityPublish ismActivityPublish = new IsmActivityPublish();
        ismActivityPublish.setActivitySn(activitySn);
        List<IsmActivityPublish>  publistList = iIsmActivityPublishService.selectIsmActivityPublishList(ismActivityPublish);
        if(publistList.size() !=1){
            return error("审核失败，查找活动失败");
        }
        long familyIntegralNew = publistList.get(0).getActivityIntegral();
        String activityTitle = publistList.get(0).getActivityTitle();

        //获取家庭账户
        IsmCustManage ismCustManage = new IsmCustManage();
        ismCustManage.setFamilySn(familySn);
        ismCustManage.setHouseholderName(holderName);
        List<IsmCustManage> custList = iIsmCustManageService.selectIsmCustIntegralByfamilyInfo(ismCustManage);
        if(custList.size() !=1){
            return error("审核失败，查找家庭账户失败");
        }
        ismCustManage = custList.get(0);
        long familyIntegralBefore = ismCustManage.getFamilyIntegral();
        long familyIntegralAfter = familyIntegralBefore + familyIntegralNew;
        ismCustManage.setFamilyIntegral(familyIntegralAfter);

        //积分日志记录
        IsmIntegralRecord ismIntegralRecord = new IsmIntegralRecord();
        ismIntegralRecord.setFamilySn(familySn);
        ismIntegralRecord.setHouseholderName(holderName);
        ismIntegralRecord.setIntegralBefore(familyIntegralBefore);
        ismIntegralRecord.setIntegralExchange(familyIntegralNew);
        ismIntegralRecord.setIntegralAfter(familyIntegralAfter);
        ismIntegralRecord.setCreateBy(getUsername());
        ismIntegralRecord.setRemark("签到审核:"+activityTitle);
        ismIntegralRecordService.insertIsmIntegralRecord(ismIntegralRecord);

        //修改审核状态
        tmp.setActivityIntegral(familyIntegralNew);
        tmp.setAuditFlat(1);
        tmp.setUpdateBy(getUsername());
        ismActivitySignService.updateIsmActivitySign(tmp);

        //修改家庭积分
        return toAjax(iIsmCustManageService.updateIsmCustManageIntegral(ismCustManage));
    }


    /**
     * 批量签到审核
     */
    @PreAuthorize("@ss.hasPermi('ism:activitySign:edit')")
    @Log(title = "签到审核", businessType = BusinessType.UPDATE)
    @PutMapping("/batchActivitySignPass/{signIds}")
    public AjaxResult batchActivitySignPass(@PathVariable Long[] signIds)
    {
        String errMsg="";
        for(int i=0;i<signIds.length;i++){
            IsmActivitySign ismActivitySign = ismActivitySignService.selectIsmActivitySignBySignId(signIds[i]);
            String activitySn = ismActivitySign.getActivitySn();
            String familySn = ismActivitySign.getFamilySn();
            String holderName = ismActivitySign.getHouseholderName();

            IsmActivitySign tmp = ismActivitySignService.selectActivitySignByfamilyInfo(ismActivitySign);
            if(tmp.getAuditFlat() !=0){
                errMsg = errMsg + "家庭编号:"+familySn+"审核失败，非待审核状态;";
                continue;
            }

            //获取活动积分
            IsmActivityPublish ismActivityPublish = new IsmActivityPublish();
            ismActivityPublish.setActivitySn(activitySn);
            List<IsmActivityPublish>  publistList = iIsmActivityPublishService.selectIsmActivityPublishList(ismActivityPublish);
            if(publistList.size() !=1){
                errMsg = errMsg + "家庭编号:"+familySn+"审核失败，查找活动失败;";
                continue;
            }
            long familyIntegralNew = publistList.get(0).getActivityIntegral();
            String activityTitle = publistList.get(0).getActivityTitle();

            //获取家庭账户
            IsmCustManage ismCustManage = new IsmCustManage();
            ismCustManage.setFamilySn(familySn);
            ismCustManage.setHouseholderName(holderName);
            List<IsmCustManage> custList = iIsmCustManageService.selectIsmCustIntegralByfamilyInfo(ismCustManage);
            if(custList.size() !=1){
                errMsg = errMsg + "家庭编号:"+familySn+"审核失败，查找家庭账户失败";
                continue;
            }
            ismCustManage = custList.get(0);
            long familyIntegralBefore = ismCustManage.getFamilyIntegral();
            long familyIntegralAfter = familyIntegralBefore + familyIntegralNew;
            ismCustManage.setFamilyIntegral(familyIntegralAfter);

            //积分日志记录
            IsmIntegralRecord ismIntegralRecord = new IsmIntegralRecord();
            ismIntegralRecord.setFamilySn(familySn);
            ismIntegralRecord.setHouseholderName(holderName);
            ismIntegralRecord.setIntegralBefore(familyIntegralBefore);
            ismIntegralRecord.setIntegralExchange(familyIntegralNew);
            ismIntegralRecord.setIntegralAfter(familyIntegralAfter);
            ismIntegralRecord.setCreateBy(getUsername());
            ismIntegralRecord.setRemark("签到审核:"+activityTitle);
            ismIntegralRecordService.insertIsmIntegralRecord(ismIntegralRecord);

            //修改审核状态
            tmp.setActivityIntegral(familyIntegralNew);
            tmp.setAuditFlat(1);
            tmp.setUpdateBy(getUsername());
            ismActivitySignService.updateIsmActivitySign(tmp);

            //修改家庭积分
            toAjax(iIsmCustManageService.updateIsmCustManageIntegral(ismCustManage));
        }
        return errMsg.equals("")?toAjax(1):error(errMsg);
    }



    /**
     * 修改签到审核
     */
    @PreAuthorize("@ss.hasPermi('ism:activitySign:edit')")
    @Log(title = "签到审核", businessType = BusinessType.UPDATE)
    @PutMapping("/updateActivitySignRefuse")
    public AjaxResult updateActivitySignRefuse(@RequestBody IsmActivitySign ismActivitySign)
    {
        IsmActivitySign tmp = ismActivitySignService.selectActivitySignByfamilyInfo(ismActivitySign);
        if(tmp.getAuditFlat() !=0){
            return error("审核失败，非待审核状态");
        }
        ismActivitySign.setAuditFlat(2);
        ismActivitySign.setUpdateBy(getUsername());
        return toAjax(ismActivitySignService.updateIsmActivitySign(ismActivitySign));
    }
}
